//
//  SWMainTabBarController.swift
//  MYSwiftWeiBo
//
//  Created by shingwai chan on 16/2/26.
//  Copyright © 2016年 ChanShingWai. All rights reserved.
//

import UIKit
//import SVProgressHUD

class SWMainTabBarController: UITabBarController , SWTabBarDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainTabrBar = SWTabBar()
        //利用kvo来改变tabbr为自定义的
        setValue(mainTabrBar, forKey: "tabBar")
        //设置所有子控制器
        setAllChildControllers()
        
        //tintColor设置整个TabBaritem的渲染颜色
        //tabBar.tintColor = UIColor.orangeColor()
        
        mainTabrBar.mainTabBarDelegate = self
        
    }
    
    //MARK ———— SWTabBarDelegate代理
    func mainTabBarCompersBtnClick() {
        
        //登陆了才能发微博
        if SWUserAccountViewModel.sharedInstance.userLogin {
            let composeVC = SWComposeController()
            //跳到发微博控制器
            presentViewController(UINavigationController(rootViewController: composeVC), animated: true, completion: nil)
        }else {
        //没登录提示用户
//            SVProgressHUD.showErrorWithStatus("登陆后才能发微博哟亲")
        }
    }
    
    //设定全部子控制器
    func setAllChildControllers() {
        //主页
        let home = SWHomeViewController()
        setUPChildController(controller: home, title: "首页", itemImageName: "tabbar_home")
        
        //消息
        let messenger = SWMessengerController()
        setUPChildController(controller: messenger, title: "消息", itemImageName: "tabbar_message_center")
        
        //加号占位View
        let plusView = UIViewController()
        setUPChildController(controller: plusView, title: " ", itemImageName: " ")
        //发现
        let discover = SWDiscoverController()
        setUPChildController(controller: discover, title: "发现", itemImageName: "tabbar_discover")
        
        //我
        let profile = SWProfileController()
        setUPChildController(controller: profile, title: "我", itemImageName: "tabbar_profile")
        
    }
    
    //设置每一个子控制器
    func setUPChildController(controller controller:UIViewController, title:String, var itemImageName:String) {
        //设置title
        controller.title = title
        //图片不渲染
        var image = UIImage(named: itemImageName)
        image =  image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //item的图片
        controller.tabBarItem.image = image
        //选中图片 //图片不渲染
        itemImageName  = itemImageName + "_selected"
        var selectedImage = UIImage(named: itemImageName)
        selectedImage = selectedImage?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        controller.tabBarItem.selectedImage = selectedImage
        //设置字体属性 为橙色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
//        controller.tabBarItem.badgeValue = "10"
        //包装nav控制器
        let navController = UINavigationController()
        navController.addChildViewController(controller)
        //加进tabbar控制器
        addChildViewController(navController)
    }


}
